package map;

import java.util.HashSet;
import java.util.Set;

public class LongestConsecutiveSequence128 {
    public int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            set.add(nums[i]);
        }
        int max = 0;

        for (Integer i : set) {
            if (!set.contains(i - 1)) {
                int count = 1;
                while (set.contains(i + 1)) {
                    i = i + 1;
                    count++;
                }
                max = max > count ? max : count;
            }
        }
        return max;
    }

}
